// import Md5 from '@/common/libs/md5.js'
import util from '@/common/js/util.js'
import {
	appConfig,
	appApi
} from '@/common/js/config.js'

/*网络请求封装*/
function request(url, data = {}, method = 'GET', loading = false,jsonPost) {
	 
	return new Promise(function(resolve, reject) {
		let  type=''
		if(jsonPost){
			type='application/json'
		}else{
			type='application/x-www-form-urlencoded'
		}	
		// console.log(uni.getStorageSync('token'))
		// console.log(uni.getStorageSync('token') || appConfig.token)
		if (loading) {
			uni.showLoading({
				title: method == 'GET' ? 'Loading' : 'Submitting'
			})
		}
		uni.request({
			url: url,
			data:data ,
			header: {
			    'Content-Type':type,
				openid: uni.getStorageSync('token') || appConfig.token,
				userId: uni.getStorageSync('userId') || appConfig.userId,
				Authorization:'eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX25hbWUiOiJ0ZXN0Iiwic2NvcGUiOlsiYWxsIl0sImlkIjoxLCJleHAiOjE2MTA2MTA4OTMsImF1dGhvcml0aWVzIjpbIuWJjeWPsOS8muWRmCJdLCJqdGkiOiJjNTAwYTQzMy04ODVkLTRkOTctYTZjYy1iNDVmYjVkMmVmMzgiLCJjbGllbnRfaWQiOiJwb3J0YWwtYXBwIn0.j7U3u7X37obI98qyvDr8OyCRupkk401kK-7zGuJexT7UKFF8gXTAfWmBjafRMjmuuCqSM-MPLQPQL1Cq8Fzj6bgS6D5Vq9TbVQ43CyE2mLU415CVlyfH0JJ35vRCj_zlgDp7SsW_ichocwfqSo0h0QlaQ6kSsXgktxpMLULIEYI'
			},
			// referer:"http://p1301.lzxlp01.ourhjd.com",
			method: method,
			dataType: 'json',
			success: function(res) {
				// console.log(res)
				if (res.statusCode == 200) {
					if (res.data.code == '200') {
						resolve(res.data)
					} else {
						// setTimeout(() => {
						// 	uni.navigateBack()
						// }, 3000)

						util.msg(res.data.message,3000)
						reject(res.data)
					}
				} else {
					// util.msg('Request failed')
					util.msg('网络出小差了')
					reject(res.data)

				}
			},
			fail: function(err) {
				// util.msg('Request timed out')
				// util.msg('网络超时了')
				// console.log(err)
				reject(err)
			},
			complete: function() {
				if (loading) {
					uni.hideLoading()
				}
			}
		})
	})
}
/*上传图片*/
function upload(src, param = {},fileType='image',url = appApi.upload) {
	return new Promise(function(resolve, reject) {
		// console.log(param)
		// console.log(src)
		// console.log(fileType)
		// console.log(url)
		// console.log({
		//     ...headerParameter('obj'),
		//     'Content-Type': 'multipart/form-data'
		//   })
		uni.uploadFile({
			url: url,
			header: {
				userId: uni.getStorageSync('userId') || appConfig.userId,
				 'content-type': 'multipart/form-data',
				Authorization:'eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX25hbWUiOiJ0ZXN0Iiwic2NvcGUiOlsiYWxsIl0sImlkIjoxLCJleHAiOjE2MTA2MTA4OTMsImF1dGhvcml0aWVzIjpbIuWJjeWPsOS8muWRmCJdLCJqdGkiOiJjNTAwYTQzMy04ODVkLTRkOTctYTZjYy1iNDVmYjVkMmVmMzgiLCJjbGllbnRfaWQiOiJwb3J0YWwtYXBwIn0.j7U3u7X37obI98qyvDr8OyCRupkk401kK-7zGuJexT7UKFF8gXTAfWmBjafRMjmuuCqSM-MPLQPQL1Cq8Fzj6bgS6D5Vq9TbVQ43CyE2mLU415CVlyfH0JJ35vRCj_zlgDp7SsW_ichocwfqSo0h0QlaQ6kSsXgktxpMLULIEYI'
			},
			formData: param,
			filePath: src,
			fileType: fileType,
			name: 'file',
			success: (res) => {
				console.log(res)
				if (res.statusCode == 200) {
					let data = JSON.parse(res.data)
					console.log(data)
					if (data.error_code == 0) {
						resolve(data.data)
					} else {
						reject(data)
					}
				} else {
					reject(res)
				}
			},
			fail: (err) => {
				console.log(err)
				reject(err)
			}
		})

	})
}
/*GET请求*/
function get(url, data = {}, loading = false) {
	return request(url, data, 'GET', loading).then(data => [null, data]).catch(err => [err, null])
}
/*POST请求*/
function post(url, data = {}, loading = false) {
	return request(url, data, 'POST', loading).then(data => [null, data]).catch(err => [err, null])
}

/*修改POST请求*/
function jsonPost(url, data = {}, loading = false) {
	return request(url, data, 'POST', loading,'jsonPost').then(data => [null, data]).catch(err => [err, null])
}
/*嵌套网页参数拼接*/
function headerParameter(type = 'obj', option = {}) {
	let token = uni.getStorageSync('token') || appConfig.token
	let newObj = {}
	let data = { ...option
	}
	data.token = token

	data.t = Math.round(new Date().getTime() / 1000) + ''
	let newArr = Object.keys(data).sort()
	for (let i = 0; i < newArr.length; i++) {
		newObj[newArr[i]] = data[newArr[i]]
	}
	let newStr = util.objToUrl(newObj) + '&key=' + appConfig.key
	data.sign = String(Md5(newStr)).toUpperCase()
	if (type == 'obj') {
		return data
	}
	if (type == 'string') {
		return util.objToUrl(data)
	}
}

export {
	get,
	post,
	jsonPost,
	upload,
	headerParameter
}
